<template>
  <div class="simple-tabs">
    <!-- Header -->
    <div class="tabs__header">
      <h2 class="tabs__title">聊天列表</h2>
      <div class="tabs__actions">
        <button class="action-button" title="搜索聊天">
          <span class="icon">🔍</span>
        </button>
        <button class="action-button action-button--primary" title="新建聊天">
          <span class="icon">➕</span>
        </button>
      </div>
    </div>

    <!-- Chat List -->
    <div class="tabs__list">
      <div class="chat-list">
        <div
          v-for="chat in chatList"
          :key="chat.id"
          :class="[
            'chat-item',
            { 'chat-item--active': activeChat === chat.id }
          ]"
        >
          <!-- Avatar -->
          <div class="chat-avatar">
            <div class="avatar-placeholder">
              {{ chat.name.charAt(0) }}
            </div>
            <div
              v-if="chat.status === 'online'"
              class="status-indicator status-indicator--online"
            ></div>
            <div
              v-else-if="chat.status === 'away'"
              class="status-indicator status-indicator--away"
            ></div>
          </div>

          <!-- Content -->
          <div class="chat-content">
            <div class="chat-header">
              <h3 class="chat-name">{{ chat.name }}</h3>
              <span class="chat-time">{{ formatTime(chat.timestamp) }}</span>
            </div>
            <div class="chat-preview">
              <p class="chat-message">{{ chat.lastMessage }}</p>
              <div
                v-if="chat.unreadCount > 0"
                class="unread-badge"
              >
                {{ chat.unreadCount > 99 ? '99+' : chat.unreadCount }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
// Props
const props = defineProps({
  chatList: {
    type: Array,
    default: () => []
  },
  activeChat: {
    type: String,
    default: ''
  },
  loading: {
    type: Boolean,
    default: false
  }
})

// Methods
const formatTime = (timestamp) => {
  if (!timestamp) return ''
  
  const date = new Date(timestamp)
  const now = new Date()
  const diff = now - date
  
  // 如果是今天
  if (diff < 24 * 60 * 60 * 1000) {
    return date.toLocaleTimeString('zh-CN', { 
      hour: '2-digit', 
      minute: '2-digit' 
    })
  }
  
  // 如果是昨天
  if (diff < 48 * 60 * 60 * 1000) {
    return '昨天 ' + date.toLocaleTimeString('zh-CN', { 
      hour: '2-digit', 
      minute: '2-digit' 
    })
  }
  
  // 其他日期
  return date.toLocaleDateString('zh-CN')
}
</script>

<style scoped>
.simple-tabs {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #fff;
}

/* Header */
.tabs__header {
  padding: 16px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
}

.tabs__title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.tabs__actions {
  display: flex;
  gap: 8px;
}

.action-button {
  width: 32px;
  height: 32px;
  border: none;
  background: #f5f5f5;
  color: #666;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.action-button:hover {
  background: #e6f7ff;
  color: #1890ff;
}

.action-button--primary {
  background: #1890ff;
  color: white;
}

.action-button--primary:hover {
  background: #096dd9;
}

.action-button .icon {
  font-size: 14px;
}

/* List */
.tabs__list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.chat-list {
  padding: 0;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.2s;
  position: relative;
  border-left: 3px solid transparent;
}

.chat-item:hover {
  background-color: #f5f5f5;
}

.chat-item--active {
  background-color: #e6f7ff;
  border-left-color: #1890ff;
}

.chat-avatar {
  position: relative;
  flex-shrink: 0;
}

.avatar-placeholder {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f0f0;
  border-radius: 50%;
  font-size: 16px;
  font-weight: 600;
  color: #666;
  border: 2px solid #e0e0e0;
}

.status-indicator {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid #fff;
}

.status-indicator--online {
  background-color: #52c41a;
}

.status-indicator--away {
  background-color: #faad14;
}

.chat-content {
  flex: 1;
  min-width: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.chat-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-time {
  font-size: 12px;
  color: #999;
  flex-shrink: 0;
  margin-left: 8px;
}

.chat-preview {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.chat-message {
  font-size: 12px;
  color: #666;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.unread-badge {
  background-color: #1890ff;
  color: white;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 10px;
  min-width: 18px;
  text-align: center;
  margin-left: 8px;
  flex-shrink: 0;
}

/* 滚动条 */
.tabs__list::-webkit-scrollbar {
  width: 6px;
}

.tabs__list::-webkit-scrollbar-track {
  background: #f5f5f5;
}

.tabs__list::-webkit-scrollbar-thumb {
  background: #d9d9d9;
  border-radius: 3px;
}

.tabs__list::-webkit-scrollbar-thumb:hover {
  background: #bfbfbf;
}
</style>